package Offer.offer;

/**
 * 二维数组中的查找
 * @author leiwenkang
 * @date 2020/4/6
 */
public class Test03二维数组中的查找 {
    public boolean containNum(int[][] matrix, int num) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }

        int rows = matrix.length;
        int columns = matrix[0].length;

        int row = 0;
        int column = columns - 1;
        while (row < rows && column >= 0) {
            if (matrix[row][column] == num) {
                return true;
            } else if (matrix[row][column] < num) {
                row++;
            } else {
                column--;
            }
        }
        return false;
    }
}
